package cc.spider.web.model;


import cc.spider.model.GlobalDefaultData;
import cc.spider.web.model.http.ICookie;
import cc.spider.web.model.http.IHeader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GlobalDefaultDataExt extends GlobalDefaultData {
    private int connectTimeout = 30000;
    private String charset = "UTF-8";

    private boolean isAutoManagementCookies = true;

    private String domain;
    private Map<String, Map<String, ICookie>> cookiesPools = new HashMap<>();
    private List<IHeader> headers = new ArrayList<>();

    public String getDomain() {
        return domain;
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }

    public Map<String, ICookie> getCookiesPool(String poolName) {
        if (poolName == null) {
            poolName = "_default";
        }
        return this.cookiesPools.computeIfAbsent(poolName, k -> new HashMap<>());
    }

    public void addCookiesPool(String poolName, Map<String, ICookie> cookies) {
        this.cookiesPools.put(poolName, cookies);
    }

    public void addCookies(String poolName, ICookie cookie) {
        this.getCookiesPool(poolName).put(cookie.getName(), cookie);
    }

    public Map<String, ICookie> getDefaultCookies() {
        return this.cookiesPools.get(null);
    }

    public List<IHeader> getHeaders() {
        return headers;
    }

    public void setHeaders(List<IHeader> headers) {
        this.headers = headers;
    }

    public void addHeaders(IHeader headers) {
        this.headers.add(headers);
    }


    public int getConnectTimeout() {
        return connectTimeout;
    }

    public void setConnectTimeout(int connectTimeout) {
        this.connectTimeout = connectTimeout;
    }

    public boolean isAutoManagementCookies() {
        return isAutoManagementCookies;
    }

    public void setAutoManagementCookies(boolean autoManagementCookies) {
        isAutoManagementCookies = autoManagementCookies;
    }


    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }
}
